package com.qf.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.qf.feign.SearchFeign;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 千锋健哥
 */
@RestController
@RequestMapping("/custom")
public class CustomController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private SearchFeign searchFeign;

    /**
     * 测试feign底层使用http协议发送请求调用
     * @return
     */
    @RequestMapping("/hello")
    public String testHello() {
        //1. 根据被调用服务的服务名称到eureka中获取, 对应的ip地址和端口号列表
        //InstanceInfo server = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);
        //2. 获取被调用微服务的ip和端口
        //String url = server.getHomePageUrl();

        //3. 远程调用, 如果使用ribbion必须使用服务名进行调用, ribbon才会生效
        String str = restTemplate.getForObject("http://QF-SEARCH/search/hello", String.class);
        return str;
    }

    /**
     * 测试feign+ribbon远程调用
     * @return
     */
    @RequestMapping("/testFeign")
    public String testFeignHello() {
        String str = searchFeign.hello();
        return  str;
    }

    @RequestMapping("/findById")
    public User findbyId(Integer id) {
        User user = searchFeign.findUserById(id);
        return user;
    }

    @RequestMapping("/insertUser")
    public String  insertUser(@RequestBody User user) {
        String str = searchFeign.insertUser(user);
        return str;
    }
}
